package com.tim.app.ui.fragment.person;

import android.content.res.Configuration;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.application.library.base.BaseFragment;
import com.application.library.net.StringResponseCallback;
import com.application.library.widget.EmptyLayout;
import com.application.library.widget.loadmore.LoadMoreContainer;
import com.application.library.widget.loadmore.LoadMoreHandler;
import com.application.library.widget.loadmore.LoadMoreRecycleViewContainer;
import com.application.library.widget.recycle.BaseRecyclerAdapter;
import com.application.library.widget.recycle.WrapRecyclerView;
import com.google.gson.Gson;
import com.lzy.okhttputils.OkHttpUtils;
import com.tim.app.R;
import com.tim.app.constant.AppKey;
import com.tim.app.server.api.API_User;
import com.tim.app.server.entry.Answer;
import com.tim.app.server.result.AnswerListResult;
import com.tim.app.server.result.ResponseResult;
import com.tim.app.ui.activity.selected.AnswerDetailActivity;
import com.tim.app.ui.adapter.personal.AnswerBrowseHistoryAdapter;
import com.tim.app.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;


/**
 * 我的回答浏览记录
 */
public class MyAnswerBrowseHistoryFragment extends BaseFragment implements View.OnClickListener, LoadMoreHandler, BaseRecyclerAdapter.OnItemClickListener {

    private static final String TAG = "MyAnswerBrowseHistoryFragment";
    private View rootView;
    private LoadMoreRecycleViewContainer load_more;
    private WrapRecyclerView wrvPersonial;
    private EmptyLayout emptyLayout;
    private AnswerBrowseHistoryAdapter adapter;
    private List<Answer> dataList;

    private int pageNum = 1;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (null == rootView) {
            rootView = inflater.inflate(R.layout.fragment_personal_view, container, false);

            load_more = (LoadMoreRecycleViewContainer) rootView.findViewById(R.id.load_more);
            wrvPersonial = (WrapRecyclerView) rootView.findViewById(R.id.wrvPersonial);

            wrvPersonial.setOverScrollMode(View.OVER_SCROLL_NEVER);

            load_more.useDefaultFooter(View.GONE);
            load_more.setAutoLoadMore(true);
            load_more.setLoadMoreHandler(this);

            emptyLayout = new EmptyLayout(getActivity(), load_more);
            emptyLayout.showLoading();
            emptyLayout.setEmptyDrawable(R.drawable.ic_empry_browser_history);
            emptyLayout.setEmptyText("你还没有浏览过任何回答", "再看看其它的吧");

            if (dataList == null) {
                dataList = new ArrayList<>();
            }
            LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
            layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            wrvPersonial.setLayoutManager(layoutManager);
            adapter = new AnswerBrowseHistoryAdapter(getActivity(), dataList);
            adapter.setOnItemClickListener(this);
            wrvPersonial.setAdapter(adapter);
        }
        initData();
        return rootView;
    }


    private void initData() {
        pageNum = 1;
        API_User.ins().getUserBrowserHistory(TAG, AppKey.TYPE_ANSWER,pageNum, AppKey.PAGE_SIZE, refreshCallback);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (null != rootView&& null != rootView.getParent()) {
            ((ViewGroup) rootView.getParent()).removeView(rootView);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        OkHttpUtils.getInstance().cancelTag(TAG);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        }
    }

    @Override
    public void onItemClick(View view, int position, long id) {
        AnswerDetailActivity.start(getContext(), dataList.get(position), false);
    }

    @Override
    public void onLoadMore(LoadMoreContainer loadMoreContainer) {
        API_User.ins().getUserBrowserHistory(TAG, AppKey.TYPE_ANSWER, pageNum + 1, AppKey.PAGE_SIZE, moreCallback);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

    StringResponseCallback refreshCallback = new StringResponseCallback() {
        @Override
        public boolean onStringResponse(String resultValue, int errcode, String errMsg, int id, boolean fromcache) {
            if (errcode == ResponseResult.ResultStatus.OK && resultValue != null) {
                if (dataList != null) {
                    dataList.clear();
                }
                AnswerListResult result = new Gson().fromJson(resultValue, AnswerListResult.class);

                if (null != result && null != result.getRes() && null != result.getRes().getArraylist()) {
                    if (null != result.getRes().getArraylist().getResult()) {
                        dataList.addAll(result.getRes().getArraylist().getResult());
                        adapter.notifyDataSetChanged();
                    }
                    pageNum = result.getRes().getArraylist().getCurrentPageNo();
                    if (pageNum != result.getRes().getArraylist().getTotalPageCount()) {
                        load_more.loadMoreFinish(false, true);
                    } else {
                        load_more.loadMoreFinish(false, false);
                    }
                }

                if (dataList == null || dataList.size() == 0) {
                    emptyLayout.showEmpty();
                } else {
                    emptyLayout.showContent();
                }
            } else {
                emptyLayout.showEmptyOrError(errcode);
                ToastUtil.showToast(errMsg);
            }
            return false;
        }
    };

    StringResponseCallback moreCallback = new StringResponseCallback() {
        @Override
        public boolean onStringResponse(String resultValue, int errcode, String errMsg, int id, boolean fromcache) {
            if (errcode == ResponseResult.ResultStatus.OK && resultValue != null) {
                AnswerListResult result = new Gson().fromJson(resultValue, AnswerListResult.class);

                if (null != result && null != result.getRes() && null != result.getRes().getArraylist()) {
                    if (null != result.getRes().getArraylist().getResult()) {
                        dataList.addAll(result.getRes().getArraylist().getResult());
                        adapter.notifyDataSetChanged();
                    }
                    pageNum = result.getRes().getArraylist().getCurrentPageNo();
                    if (pageNum == result.getRes().getArraylist().getTotalPageCount()) {
                        load_more.loadMoreFinish(false, true);
                    } else {
                        load_more.loadMoreFinish(false, false);
                    }
                }

            } else {
                emptyLayout.showEmptyOrError(errcode);
                ToastUtil.showToast(errMsg);
            }
            return false;
        }
    };

}
